<?php
/***
*
* Class: CartService //购物车服务
* Author: Jason ZHENG
* Data: 2018-07-18
*
***/
namespace App\Service;

use App\Service\DataReturnService as DRS;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session; 
use App\Common\Base;

class CartService
{
	/**
     * 2018-06-28
     * czw
     * @param ids array 多个id以逗号隔开
     * 验证该用户的购物车是否存在这个产品
     */
    public static function check_cart($ids,$user_id)
    {
       $where = [
            'user_id' => $user_id,
            'delete' => 0, // 没有删除
       ];
       $row_cart = DB::table('cart')->where($where)->whereIn('id',$ids)->get()->toarray();
       if(!$row_cart){
           return FALSE;
       }else{
           return TRUE;
       }
    }

    /**
     * 2018-05-31
     * czw
     * 根据产品品牌和系列生成产品名称
     */
    public static function set_product_name($systematics_id = 0){
        $systematics_arr = self::get_systematics($systematics_id);
        if(array_key_exists('xilie', $systematics_arr)){
            $product_name = $systematics_arr['brand']['title'].'/'.$systematics_arr['xilie']['title'];
        }elseif(array_key_exists('brand', $systematics_arr)){
            $product_name = $systematics_arr['brand']['title'];
        }else{
            $product_name = '';
        }
        return $product_name;
    }

    /**
     * 2018-05-31
     * czw 改
     * @param type $systematics
     * @return int
     */
    public static function get_systematics($systematics){
        $date = array();
        if($systematics){
            $rowQuery = Db::table('systematics')->where('id',$systematics)->select('id','superior_id','superior_gene','title')->first();
            $row = (array)$rowQuery;
            $superior_id = $row['superior_id'];
            $superior_gene = $row['superior_gene'];
            $superior_gene_arr = explode('-',$superior_gene);
            if($superior_gene){
                foreach ($superior_gene_arr as $sk => $sv) {
                    $row_syQuery = Db::table('systematics')->where('id',$sv)->select('id','title')->first();
                    $row_sy = (array)$row_syQuery;
                    if($sk == 0){
                        $date['systematics'] = $row_sy;
                        $date['child_systematics']['id'] = $row['id'];
                        $date['child_systematics']['title'] = $row['title'];
                    }elseif ($sk == 1) {
                        $date['child_systematics'] = $row_sy;
                        $date['brand']['id'] = $row['id'];
                        $date['brand']['title'] = $row['title'];
                    }elseif($sk == 2){
                        $date['brand'] = $row_sy;
                        $date['xilie']['id'] = $row['id'];
                        $date['xilie']['title'] = $row['title'];
                    }elseif($sk == 3){
                        $date['xilie'] = $row_sy;
                    }
                }
            }else{
                $sysQuery =  Db::table('systematics')->where('id',$systematics)->select('id','title')->first();
                $date['systematics'] = (array)$sysQuery;
            }
        }else{
            $date = [];
        }
        return $date;
    }


}